package znbkListSc.presenter;


import android.util.Log;

import com.example.lancooznbk.R;
import znbkListSc.api.CollectionListApi;
import znbkListSc.entity.CollectionListEntity;
import znbkListSc.ICollectionPresenter;
import com.example.lancooznbk.view.base.BaseFragment;
import znbkListSc.ICollectionListView;
import com.lancoo.reviewsystem.utils.ToastUtils;
import com.lancoo.rxretrofitlibrary.Api.UserInfo;
import com.lancoo.rxretrofitlibrary.http.HttpManager;
import com.lancoo.rxretrofitlibrary.listener.HttpOnNextListener;
import com.trello.rxlifecycle.components.support.RxFragment;

import java.util.List;

import znbkChangeCollection.ChangeCollectionApi.ChangeCollectionApi;
import znbkChangeCollection.ChangeCollectionEntityModel;
import znbkTestPaperInfor.ZnbkPaperDef;
import znbkTestPaperSc.activity.CollectionPaperActivity;
import znbkTestPaperSc.activity.CollectionPaperSplActivity;

/**
 * Created by Administrator on 2018\2\9 0009.
 */

public class CollectionListPresenter implements ICollectionPresenter{

    private ICollectionListView mView;
    private int lastPosition = 0;

    private int pageIndex = 1;
    private boolean fromSearch = false;

    private HttpOnNextListener<CollectionListEntity> getCollectionListener = new HttpOnNextListener<CollectionListEntity>() {
        @Override
        public void onNext(CollectionListEntity collectionListEntity) {
            if (collectionListEntity.getTotalCount() > 0){
                mView.hideLoading();

                List<CollectionListEntity.FavouriteBean> modelList = collectionListEntity.getFavourite();
                for (int i = 0 ; i < modelList.size() ; i ++){ //预处理题库返回不合理的地方
                    String summary = modelList.get(i).getTheme();
                    String regexStr = "<[^>]*>"; //去除所有的标签
                    summary = summary.replaceAll(regexStr, "");

                  /*  summary = TagHelperUtils.replacAllTag(summary,"<P>","");
                    summary = TagHelperUtils.replacAllTag(summary,"<BR>","");
                    summary = TagHelperUtils.replaceNULL_U_Tag(summary);
                    summary = TagHelperUtils.replaceImageTag(summary);
                    if (summary.length() >100 &&
                            !(summary.contains("<table") || summary.contains("<TABLE"))){
                        summary = summary.substring(0,100);
                    }*/
                  if(summary.equals("")){
                      summary = "暂无简介";
                  }else {
                      if (summary.length() > 100) {
                          summary = summary.substring(0, 100);
                      }
                  }

                    modelList.get(i).setTheme(summary);
                }

                if (pageIndex == 1) {
                    mView.loadData(collectionListEntity);
                    lastPosition = 0;
                }else {
                    mView.loadMoreData(collectionListEntity);
                }


            }else {
                mView.showNoData(fromSearch ? mView.getHolderActivity().getString(R.string.search_no_favourite_list):
                        mView.getHolderActivity().getString(R.string.no_favourite_list));
            }
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            mView.showNetError();
        }
    };
    private HttpOnNextListener<String> changeCollectionListener = new HttpOnNextListener<String>() {

        @Override
        public void onNext(String s) {
            if(s.equals("true")){
                mView.delFavouriteSucceed();
            }else{
                ToastUtils.showShort(mView.getHolderActivity().getString(R.string.del_failed));
            }
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
//            Log.e("===222222000000==","");
//            ToastUtils.showShort(mView.getHolderActivity().getString(R.string.del_failed22));
        }
    };

    public CollectionListPresenter(ICollectionListView mView) {
        this.mView = mView;
    }

    private void loadData(){
        fromSearch = false;

        CollectionListApi api = new CollectionListApi(getCollectionListener, (RxFragment) mView);
        api.setResType("");
        api.setSearchInfo("");
        api.setPageNumber(pageIndex);
        api.setPageSize(lastPosition + 10);
        HttpManager httpManager = HttpManager.getInstance();
        httpManager.doHttpDeal(api);
    }

    @Override
    public void getData(boolean isShowing) {
        if (isShowing) mView.showLoading();
        pageIndex = 1;
        loadData();
    }

    @Override
    public void getMoreData() {
        pageIndex = pageIndex + 1;
        loadData();
    }

    @Override
    public void searchFavouriteItem(String str) {
        fromSearch = true;
        mView.showLoading();
        pageIndex = 1;
        CollectionListApi api = new CollectionListApi(getCollectionListener, (RxFragment) mView);
        api.setResType("");
        api.setSearchInfo(str);
        api.setPageNumber(pageIndex);
        api.setPageSize(lastPosition + 10);
        HttpManager httpManager = HttpManager.getInstance();
        httpManager.doHttpDeal(api);
    }



    @Override
    public void delItemFavourite(int position, CollectionListEntity.FavouriteBean favouriteBean) {
        ChangeCollectionApi api = new ChangeCollectionApi(changeCollectionListener, (BaseFragment) mView);
        ChangeCollectionEntityModel changeCollectionEntityModel = new ChangeCollectionEntityModel(0,favouriteBean.getGenreID(),
                favouriteBean.getQuesID(),favouriteBean.getResType(),UserInfo.StageNo,UserInfo.StuId,UserInfo.SubjectId,
                favouriteBean.getTheme(), favouriteBean.getTypeNo());
        api.setmChangeCollectionEntityModel(changeCollectionEntityModel);
        HttpManager httpManager = HttpManager.getInstance();
        httpManager.doHttpDeal(api);
    }

    @Override
    public void openFavoriteItemDetail(int position, CollectionListEntity.FavouriteBean favouriteBean) {
        lastPosition = position;
        //TODO 在这里实现跳转逻辑 如果想传递唯一标识的参数可以传递 @PaperDef int FROM_FAVOURITE_REVIEW
        Log.e("========QuesID======",""+favouriteBean.getQuesID());

        if(favouriteBean.getResType().equals("4")||
                favouriteBean.getResType().equals("6")){
            CollectionPaperSplActivity.launch((BaseFragment) mView, favouriteBean, ZnbkPaperDef.FROM_COLLECTION);
        }else {
            CollectionPaperActivity.launch((BaseFragment) mView, favouriteBean, ZnbkPaperDef.FROM_COLLECTION);
        }

    }
}